from tracemalloc import take_snapshot

import jieba
txt = open("西游记.txt","rb").read()

word = jieba.lcut(txt)  #将分词结果直接转化为列表
def takesecond(el):
    return el[1]

'''
孙悟空   次数
猪八戒   次数
沙僧   次数
唐僧   次数

'''
# word = ["孙悟空","猪八戒","猪八戒","孙悟空","孙悟空","孙悟空","沙僧","沙僧","唐僧","唐僧"]
words = {} #创建字典用于存储统计结果

for w in word:

    words[w] = words.get(w,0) + 1;


items = list(words.items())   #将字典转换成元组，再将元组转换成列表，为了方便排序。

items.sort(key=takesecond,reverse=True)  #利用函数对第二个数进行排序
print(items)




#
# print(words.keys())

